﻿namespace UglyToad.PdfPig.Tests.Integration
{
    using Content;

    public class MultiplePageMortalityStatisticsTests
    {
        private static string GetFilename()
        {
            return IntegrationHelpers.GetDocumentPath("Multiple Page - from Mortality Statistics.pdf");
        }

        [Fact]
        public void HasCorrectNumberOfPages()
        {
            var file = GetFilename();

            using (var document = PdfDocument.Open(File.ReadAllBytes(file)))
            {
                Assert.Equal(6, document.NumberOfPages);
            }
        }

        [Fact]
        public void HasCorrectVersion()
        {
            using (var document = PdfDocument.Open(GetFilename()))
            {
                Assert.Equal(1.7, document.Version);
            }
        }

        [Fact]
        public void GetsFirstPageContent()
        {
            using (var document = PdfDocument.Open(GetFilename()))
            {
                var page = document.GetPage(1);

                Assert.Contains("Mortality Statistics: Metadata", page.Text);
                Assert.Contains("Notification to the registrar by the coroner that he does not consider it necessary to hold an inquest – no post-mortem held (Form 100A – salmon pink)", page.Text);
                Assert.Contains("Presumption of death certificate", page.Text);

                Assert.Equal(PageSize.Letter, page.Size);
            }
        }

        [Fact]
        public void GetsPagesContent()
        {
            using (var document = PdfDocument.Open(GetFilename()))
            {
                var pages = new[]
                {
                    document.GetPage(1),
                    document.GetPage(2),
                    document.GetPage(3),
                    document.GetPage(4),
                    document.GetPage(5),
                    document.GetPage(6)
                };

                Assert.Contains(@"Up to 1992, publications gave numbers of deaths registered in the period concerned. From 1993 to 2005, the figures in annual reference volumes relate to the number of deaths that "
+ "occurred in the reference period. From 2006 onwards, all tables in Series DR are based on "
+ "deaths registered in a calendar period. More details on these changes can be found in the "
+ "publication Mortality Statistics: Deaths Registered in 2006 (ONS, 2008)", pages[5].Text);
            }
        }

        [Fact]
        public void LettersHaveCorrectPositionsPdfBox()
        {
            using (var document = PdfDocument.Open(GetFilename()))
            {
                var page = document.GetPage(1);

                var letters = page.Letters;
                var positions = GetPdfBoxPositions();

                for (var i = 0; i < positions.Count; i++)
                {
                    var letter = letters[i];
                    var position = positions[i];

                    position.AssertWithinTolerance(letter, page, false);
                }
            }
        }

        private static IReadOnlyList<AssertablePositionData> GetPdfBoxPositions()
        {
            const string data = @"390.6	741.12	8.29668	M	9.96	ArialMT	14.601361
398.87976	741.12	5.5377607	o	9.96	ArialMT	11.0556
404.39957	741.12	3.31668	r	9.96	ArialMT	10.816561
407.76007	741.12	2.7688804	t	9.96	ArialMT	14.41212
410.52	741.12	5.5377607	a	9.96	ArialMT	11.0556
416.1603	741.12	2.2111201	l	9.96	ArialMT	14.601361
418.32162	741.12	2.2111201	i	9.96	ArialMT	14.601361
420.48294	741.12	2.7688804	t	9.96	ArialMT	14.41212
423.4839	741.12	4.98	y	9.96	ArialMT	14.87028
428.28	741.12	2.7688804	 	9.96	ArialMT	0.0
431.16	741.12	6.6433206	S	9.96	ArialMT	15.09936
437.75952	741.12	2.7688804	t	9.96	ArialMT	14.41212
440.52045	741.12	5.5377607	a	9.96	ArialMT	11.0556
446.0383	741.12	2.7688804	t	9.96	ArialMT	14.41212
448.91772	741.12	2.2111201	i	9.96	ArialMT	14.601361
451.07803	741.12	4.98	s	9.96	ArialMT	11.0556
456.1178	741.12	2.7688804	t	9.96	ArialMT	14.41212
458.87772	741.12	2.2111201	i	9.96	ArialMT	14.601361
461.03802	741.12	4.98	c	9.96	ArialMT	11.0556
466.0778	741.12	4.98	s	9.96	ArialMT	11.0556
471.11755	741.12	2.7688804	:	9.96	ArialMT	10.57752
473.87747	741.12	2.7688804	 	9.96	ArialMT	0.0
476.63638	741.12	8.29668	M	9.96	ArialMT	14.601361
485.03564	741.12	5.5377607	e	9.96	ArialMT	11.0556
490.5535	741.12	2.7688804	t	9.96	ArialMT	14.41212
493.31342	741.12	5.5377607	a	9.96	ArialMT	11.0556
498.83127	741.12	5.5377607	d	9.96	ArialMT	14.840402
504.47064	741.12	5.5377607	a	9.96	ArialMT	11.0556
509.9885	741.12	2.7688804	t	9.96	ArialMT	14.41212
512.8689	741.12	5.5377607	a	9.96	ArialMT	11.0556
518.4	741.12	2.7688804	 	9.96	ArialMT	0.0
571.92	741.36	2.5020003	 	9.0	ArialMT	0.0
90.0	721.44	3.0	 	12.0	TimesNewRomanPSMT	0.0
90.0	708.24	3.0691202	 	11.04	ArialMT	0.0
126.001434	708.24	6.1382403	4	11.04	ArialMT	16.18464
132.1176	708.24	3.0691202	.	11.04	ArialMT	2.2632
135.23862	708.24	6.1382403	3	11.04	ArialMT	16.53792
141.23663	708.24	3.0691202	 	11.04	ArialMT	0.0
161.99184	708.24	7.3636804	E	11.04	ArialMT	16.18464
169.31136	708.24	6.1382403	n	11.04	ArialMT	11.989441
175.42752	708.24	3.0691202	t	11.04	ArialMT	15.97488
178.54742	708.24	3.67632	r	11.04	ArialMT	11.989441
182.2679	708.24	5.52	y	11.04	ArialMT	16.48272
187.66757	708.24	3.0691202	 	11.04	ArialMT	0.0
190.79189	708.24	6.1382403	o	11.04	ArialMT	12.2544
196.79213	708.24	3.0691202	f	11.04	ArialMT	16.46064
199.91203	708.24	3.0691202	 	11.04	ArialMT	0.0
203.03635	708.24	6.1382403	d	11.04	ArialMT	16.449602
209.15251	708.24	6.1382403	a	11.04	ArialMT	12.2544
215.26868	708.24	3.0691202	t	11.04	ArialMT	15.97488
218.38858	708.24	6.1382403	a	11.04	ArialMT	12.2544
224.38773	708.24	3.0691202	 	11.04	ArialMT	0.0
227.39279	708.24	3.0691202	 	11.04	ArialMT	0.0
233.9947	708.24	3.0691202	 	11.04	ArialMT	0.0
269.99615	708.24	3.0691202	 	11.04	ArialMT	0.0
305.9976	708.24	3.0691202	 	11.04	ArialMT	0.0
341.99902	708.24	3.0691202	 	11.04	ArialMT	0.0
378.00046	708.24	3.0691202	 	11.04	ArialMT	0.0
414.0019	708.24	3.0691202	 	11.04	ArialMT	0.0
450.00333	708.24	3.0691202	 	11.04	ArialMT	0.0
486.00476	708.24	6.1382403	3	11.04	ArialMT	16.53792
492.1209	708.24	6.1382403	6	11.04	ArialMT	16.52688
498.23706	708.24	3.0691202	 	11.04	ArialMT	0.0
521.9951	708.24	3.0691202	 	11.04	ArialMT	0.0
162.0	695.52	3.6984	•	8.04	LYVNDC+SymbolMT	0.008040001
165.72252	695.52	2.2351203	 	8.04	ArialMT	0.0
180.0	695.52	7.9708805	R	11.04	ArialMT	16.18464
187.9201	695.52	6.1382403	e	11.04	ArialMT	12.2544
194.03627	695.52	6.1382403	g	11.04	ArialMT	16.74768
200.27719	695.52	2.4508803	i	11.04	ArialMT	16.18464
202.6762	695.52	5.52	s	11.04	ArialMT	12.2544
208.1962	695.52	3.0691202	t	11.04	ArialMT	15.97488
211.19687	695.52	3.67632	r	11.04	ArialMT	11.989441
214.91624	695.52	6.1382403	a	11.04	ArialMT	12.2544
221.03241	695.52	3.0691202	t	11.04	ArialMT	15.97488
224.15231	695.52	2.4508803	i	11.04	ArialMT	16.18464
226.55241	695.52	6.1382403	o	11.04	ArialMT	12.2544
232.66858	695.52	6.1382403	n	11.04	ArialMT	11.989441
238.78474	695.52	3.0691202	 	11.04	ArialMT	0.0
241.90906	695.52	7.3636804	S	11.04	ArialMT	16.73664
249.22858	695.52	6.1382403	e	11.04	ArialMT	12.2544
255.22882	695.52	3.67632	r	11.04	ArialMT	11.989441
258.9493	695.52	5.52	v	11.04	ArialMT	11.724481
264.34897	695.52	2.4508803	i	11.04	ArialMT	16.18464
266.74905	695.52	5.52	c	11.04	ArialMT	12.2544
272.26907	695.52	6.1382403	e	11.04	ArialMT	12.2544
278.38522	695.52	3.0691202	 	11.04	ArialMT	0.0
281.50955	695.52	7.3636804	S	11.04	ArialMT	16.73664
288.8302	695.52	6.1382403	o	11.04	ArialMT	12.2544
294.8293	695.52	3.0691202	f	11.04	ArialMT	16.46064
298.06955	695.52	3.0691202	t	11.04	ArialMT	15.97488
301.0691	695.52	7.9708805	w	11.04	ArialMT	11.724481
308.86993	695.52	6.1382403	a	11.04	ArialMT	12.2544
314.98608	695.52	3.67632	r	11.04	ArialMT	11.989441
318.70657	695.52	6.1382403	e	11.04	ArialMT	12.2544
324.82272	695.52	3.0691202	 	11.04	ArialMT	0.0
327.94705	695.52	3.67632	(	11.04	ArialMT	21.21888
331.66754	695.52	7.9708805	R	11.04	ArialMT	16.18464
339.58762	695.52	7.3636804	S	11.04	ArialMT	16.73664
346.90714	695.52	7.3636804	S	11.04	ArialMT	16.73664
354.22778	695.52	3.67632	)	11.04	ArialMT	21.21888
357.9648	695.52	3.0691202	 	11.04	ArialMT	0.0
162.0	682.92	3.6984	•	8.04	LYVNDC+SymbolMT	0.008040001
165.72252	682.92	2.2351203	 	8.04	ArialMT	0.0
180.0	682.92	7.9708805	R	11.04	ArialMT	16.18464
187.9201	682.92	6.1382403	e	11.04	ArialMT	12.2544
194.03627	682.92	6.1382403	g	11.04	ArialMT	16.74768
200.27719	682.92	2.4508803	i	11.04	ArialMT	16.18464
202.6762	682.92	5.52	s	11.04	ArialMT	12.2544
208.1962	682.92	3.0691202	t	11.04	ArialMT	15.97488
211.19687	682.92	3.67632	r	11.04	ArialMT	11.989441
214.91624	682.92	6.1382403	a	11.04	ArialMT	12.2544
221.03241	682.92	3.0691202	t	11.04	ArialMT	15.97488
224.15231	682.92	2.4508803	i	11.04	ArialMT	16.18464
226.55241	682.92	6.1382403	o	11.04	ArialMT	12.2544
232.66858	682.92	6.1382403	n	11.04	ArialMT	11.989441
238.78474	682.92	3.0691202	 	11.04	ArialMT	0.0
241.78763	682.92	8.589121	O	11.04	ArialMT	16.74768
250.42754	682.92	6.1382403	n	11.04	ArialMT	11.989441
256.5437	682.92	2.4508803	l	11.04	ArialMT	16.18464
258.9427	682.92	2.4508803	i	11.04	ArialMT	16.18464
261.34277	682.92	6.1382403	n	11.04	ArialMT	11.989441
267.45892	682.92	6.1382403	e	11.04	ArialMT	12.2544
273.57507	682.92	3.0691202	 	11.04	ArialMT	0.0
276.69498	682.92	3.67632	(	11.04	ArialMT	21.21888
280.41547	682.92	7.9708805	R	11.04	ArialMT	16.18464
288.2152	682.92	8.589121	O	11.04	ArialMT	16.74768
296.7359	682.92	7.9708805	N	11.04	ArialMT	16.18464
304.65598	682.92	3.67632	)	11.04	ArialMT	21.21888
308.3952	682.92	3.0691202	 	11.04	ArialMT	0.0
89.990875	670.2019	3.0691202	 	11.04	ArialMT	0.0
125.99231	670.2019	6.1382403	4	11.04	ArialMT	16.18464
132.10847	670.2019	3.0691202	.	11.04	ArialMT	2.2632
135.22838	670.2019	6.1382403	4	11.04	ArialMT	16.18464
141.22751	670.2019	3.0691202	 	11.04	ArialMT	0.0
161.98271	670.2019	8.589121	O	11.04	ArialMT	16.74768
170.62262	670.2019	3.0691202	t	11.04	ArialMT	15.97488
173.74252	670.2019	6.1382403	h	11.04	ArialMT	16.18464
179.85869	670.2019	6.1382403	e	11.04	ArialMT	12.2544
185.85783	670.2019	3.67632	r	11.04	ArialMT	11.989441
189.57831	670.2019	3.0691202	 	11.04	ArialMT	0.0
192.70262	670.2019	5.52	c	11.04	ArialMT	12.2544
198.22263	670.2019	6.1382403	h	11.04	ArialMT	16.18464
204.33879	670.2019	6.1382403	e	11.04	ArialMT	12.2544
210.33904	670.2019	5.52	c	11.04	ArialMT	12.2544
215.7398	670.2019	5.52	k	11.04	ArialMT	16.18464
221.37904	670.2019	5.52	s	11.04	ArialMT	12.2544
226.89905	670.2019	3.0691202	 	11.04	ArialMT	0.0
229.8986	670.2019	9.196321	m	11.04	ArialMT	11.989441
239.13908	670.2019	6.1382403	a	11.04	ArialMT	12.2544
245.25525	670.2019	6.1382403	d	11.04	ArialMT	16.449602
251.37141	670.2019	6.1382403	e	11.04	ArialMT	12.2544
257.37167	670.2019	3.0691202	 	11.04	ArialMT	0.0
260.496	670.2019	6.1382403	b	11.04	ArialMT	16.449602
266.61215	670.2019	5.52	y	11.04	ArialMT	16.48272
272.01294	670.2019	3.0691202	 	11.04	ArialMT	0.0
275.13727	670.2019	3.0691202	t	11.04	ArialMT	15.97488
278.25717	670.2019	6.1382403	h	11.04	ArialMT	16.18464
284.2563	670.2019	6.1382403	e	11.04	ArialMT	12.2544
290.37244	670.2019	3.0691202	 	11.04	ArialMT	0.0
293.49677	670.2019	7.9708805	R	11.04	ArialMT	16.18464
301.41684	670.2019	6.1382403	e	11.04	ArialMT	12.2544
307.533	670.2019	6.1382403	g	11.04	ArialMT	16.74768
313.7728	670.2019	2.4508803	i	11.04	ArialMT	16.18464
316.17288	670.2019	5.52	s	11.04	ArialMT	12.2544
321.57367	670.2019	3.0691202	t	11.04	ArialMT	15.97488
324.69357	670.2019	3.67632	r	11.04	ArialMT	11.989441
328.41293	670.2019	6.1382403	a	11.04	ArialMT	12.2544
334.41318	670.2019	3.0691202	t	11.04	ArialMT	15.97488
337.53308	670.2019	2.4508803	i	11.04	ArialMT	16.18464
339.93317	670.2019	6.1382403	o	11.04	ArialMT	12.2544
346.04932	670.2019	6.1382403	n	11.04	ArialMT	11.989441
352.16547	670.2019	3.0691202	 	11.04	ArialMT	0.0
355.2898	670.2019	7.3636804	S	11.04	ArialMT	16.73664
362.6093	670.2019	6.1382403	e	11.04	ArialMT	12.2544
368.72546	670.2019	3.67632	r	11.04	ArialMT	11.989441
372.44482	670.2019	5.52	v	11.04	ArialMT	11.724481
377.8456	670.2019	2.4508803	i	11.04	ArialMT	16.18464
380.2457	670.2019	5.52	c	11.04	ArialMT	12.2544
385.76572	670.2019	6.1382403	e	11.04	ArialMT	12.2544
391.88187	670.2019	3.0691202	 	11.04	ArialMT	0.0
394.90463	670.2019	3.0691202	 	11.04	ArialMT	0.0
413.98175	670.2019	3.0691202	 	11.04	ArialMT	0.0
449.9832	670.2019	3.0691202	 	11.04	ArialMT	0.0
485.98462	670.2019	6.1382403	3	11.04	ArialMT	16.53792
492.10077	670.2019	6.1382403	7	11.04	ArialMT	15.97488
498.21692	670.2019	3.0691202	 	11.04	ArialMT	0.0
501.2198	670.2019	3.0691202	 	11.04	ArialMT	0.0
521.975	670.2019	3.0691202	 	11.04	ArialMT	0.0
162.0	657.6	3.6984	•	8.04	LYVNDC+SymbolMT	0.008040001
165.72252	657.6	2.2351203	 	8.04	ArialMT	0.0
180.0	657.6	7.3636804	A	11.04	ArialMT	16.18464
187.31952	657.6	3.0691202	t	11.04	ArialMT	15.97488
190.44383	657.6	3.0691202	 	11.04	ArialMT	0.0
193.56374	657.6	3.0691202	t	11.04	ArialMT	15.97488
196.68805	657.6	6.1382403	h	11.04	ArialMT	16.18464
202.80862	657.6	6.1382403	e	11.04	ArialMT	12.2544
208.80885	657.6	3.0691202	 	11.04	ArialMT	0.0
211.81174	657.6	3.0691202	t	11.04	ArialMT	15.97488
214.93605	657.6	2.4508803	i	11.04	ArialMT	16.18464
217.33614	657.6	9.196321	m	11.04	ArialMT	11.989441
226.57552	657.6	6.1382403	e	11.04	ArialMT	12.2544
232.69609	657.6	3.0691202	 	11.04	ArialMT	0.0
235.816	657.6	6.1382403	o	11.04	ArialMT	12.2544
241.81622	657.6	3.0691202	f	11.04	ArialMT	16.46064
244.94054	657.6	3.0691202	 	11.04	ArialMT	0.0
247.94342	657.6	3.67632	r	11.04	ArialMT	11.989441
251.6639	657.6	6.1382403	e	11.04	ArialMT	12.2544
257.66302	657.6	6.1382403	g	11.04	ArialMT	16.74768
263.90283	657.6	2.4508803	i	11.04	ArialMT	16.18464
266.30292	657.6	5.52	s	11.04	ArialMT	12.2544
271.8229	657.6	3.0691202	t	11.04	ArialMT	15.97488
274.82358	657.6	3.67632	r	11.04	ArialMT	11.989441
278.54294	657.6	6.1382403	a	11.04	ArialMT	12.2544
284.6635	657.6	3.0691202	t	11.04	ArialMT	15.97488
287.78784	657.6	2.4508803	i	11.04	ArialMT	16.18464
290.18683	657.6	6.1382403	o	11.04	ArialMT	12.2544
296.3074	657.6	6.1382403	n	11.04	ArialMT	11.989441
302.27905	657.6	3.0691202	 	11.04	ArialMT	0.0
162.0	645.0	3.6984	•	8.04	LYVNDC+SymbolMT	0.008040001
165.72252	645.0	2.2351203	 	8.04	ArialMT	0.0
180.0	645.0	7.3636804	B	11.04	ArialMT	16.18464
187.31952	645.0	5.52	y	11.04	ArialMT	16.48272
192.72029	645.0	3.0691202	 	11.04	ArialMT	0.0
195.8446	645.0	5.52	s	11.04	ArialMT	12.2544
201.36461	645.0	6.1382403	u	11.04	ArialMT	11.989441
207.48077	645.0	6.1382403	p	11.04	ArialMT	16.48272
213.59694	645.0	6.1382403	e	11.04	ArialMT	12.2544
219.7131	645.0	3.67632	r	11.04	ArialMT	11.989441
223.43358	645.0	2.4508803	i	11.04	ArialMT	16.18464
225.83368	645.0	6.1382403	n	11.04	ArialMT	11.989441
231.94984	645.0	3.0691202	t	11.04	ArialMT	15.97488
235.06975	645.0	6.1382403	e	11.04	ArialMT	12.2544
241.18591	645.0	6.1382403	n	11.04	ArialMT	11.989441
247.30208	645.0	6.1382403	d	11.04	ArialMT	16.449602
253.41824	645.0	6.1382403	e	11.04	ArialMT	12.2544
259.5344	645.0	6.1382403	n	11.04	ArialMT	11.989441
265.65054	645.0	3.0691202	t	11.04	ArialMT	15.97488";

            var positions = data.Split(new[] {"\r", "\r\n", "\n"}, StringSplitOptions.RemoveEmptyEntries)
                .Select(AssertablePositionData.Parse)
                .ToList();

            return positions;
        }
    }
}
